﻿//-----------------------------------------------------------------------
// <copyright file="Start.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Mark Phillips</author>
// <email>andyr</email>
// <date>2004-03-23</date>
// <summary>Starts an SMTP server.</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.Web.Smtp
{
    using System;
    using System.Xml;
    using System.Globalization;
    using System.Net;
    using Microsoft.Sdc.Tasks.Configuration.Web;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Starts an SMTP Server.
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<Web.Smtp.Start
    ///     Description="description" />]]></code>
    /// <para>where:</para>
    /// <para><i>description (Required)</i></para>
    /// <para>A description for the SMTP site to be started.</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Web.Smtp.Start
    ///             Description="TestSMTP"
    ///            />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>    
    public class Start : TaskBase
    {
        private string description;
        private Microsoft.Sdc.Tasks.Configuration.Web.SmtpService smtp = null;

        /// <summary>
        /// Initializes a new instance of the Smtp class.
        /// </summary>
        public Start()
        {
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            #region Execute code

            if (!Microsoft.Sdc.Tasks.Configuration.Web.SmtpService.Exists(description))
            {
                Log.LogMessage(MessageImportance.Low, String.Format("The SMTP server with description {0} does not exist.", description));
            }
            else
            {
                smtp = SmtpService.Load(description);
                smtp.Start();
            }
            #endregion
        }

        /// <summary>
        /// SMTP server description
        /// </summary>
        /// <value>Any valid description for the SMTP server</value>
        [Required]
        public string Description
        {
            get
            {
                return (description == null ? String.Empty : description);
            }
            set
            {
                description = value;
            }
        }
    }
}